package admin

import (
	"net/http"

	"gitee.com/Gakusyun/gakusyun-ws/templates"
	"gitee.com/Gakusyun/gakusyun-ws/utils"
)

// AdminHandler 管理界面处理器
type AdminHandler struct {
	stats  *utils.ServerStats
	logger any
}

// NewAdminHandler 创建新的管理界面处理器
func NewAdminHandler(stats *utils.ServerStats, logger any) *AdminHandler {
	return &AdminHandler{
		stats:  stats,
		logger: logger,
	}
}

// AboutHandler 处理关于页面请求
func (h *AdminHandler) AboutHandler(w http.ResponseWriter, r *http.Request) {
	content, err := templates.RenderAbout()
	if err != nil {
		utils.LogWithPrintf(h.logger, "Failed to render about template: %v", err)
		utils.ErrorResponse(w, "Internal Server Error", http.StatusInternalServerError)
		return
	}

	w.Header().Set("Content-Type", "text/html")
	w.Write([]byte(content))

	utils.LogWithPrintf(h.logger, "Served about page")
}

// StatsHandler 处理统计页面请求
func (h *AdminHandler) StatsHandler(w http.ResponseWriter, r *http.Request) {
	content, err := templates.RenderStats(h.stats)
	if err != nil {
		utils.LogWithPrintf(h.logger, "Failed to render stats template: %v", err)
		utils.ErrorResponse(w, "Internal Server Error", http.StatusInternalServerError)
		return
	}

	w.Header().Set("Content-Type", "text/html")
	w.Write([]byte(content))
}